package com.hspedu.newData.dmsxl.滑动窗口;

import org.junit.Test;

import java.util.HashMap;
import java.util.HashSet;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-29 
 * @Version: 1.0
 */

public class LC2799统计完全子数组的数目 {

    public int countCompleteSubarrays(int[] nums) {

        if (nums.length == 1) return 1;

        HashSet<Integer> set = new HashSet<>();
        HashMap<Integer,Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            set.add(nums[i]);
        }

        int count = set.size();

        int res = 0;

        int slow = 0;
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i],map.getOrDefault(nums[i],0) + 1);
            while (map.size() == count){
                res += nums.length - i;
                map.put(nums[slow],map.get(nums[slow]) - 1);
                if (map.get(nums[slow]) == 0){
                    map.remove(nums[slow]);
                }
                slow++;
            }
        }

        return res;
    }

    @Test
    public void test() {
        // 测试代码

        int i = countCompleteSubarrays(new int[]{5,5,5,5});
        System.out.println(i);
    }

}
